package com.shengqi.xinnongren.work.ui.home.fragments;


import android.animation.Animator;
import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.view.animation.DecelerateInterpolator;
import android.widget.LinearLayout;

import com.shengqi.xinnongren.R;
import com.shengqi.xinnongren.common.base.BaseMvpFragment;
import com.shengqi.xinnongren.common.utils.ArrayUtil;
import com.shengqi.xinnongren.work.ui.home.activities.SearchActivity;
import com.shengqi.xinnongren.work.ui.home.adapter.ChooseSubClassAdapter;
import com.shengqi.xinnongren.work.ui.home.fragments.home2sub.GoodsListFragment;
import com.shengqi.xinnongren.work.ui.home.model.ClassBean;
import com.shengqi.xinnongren.work.ui.home.presenter.HomePage2Presenter;
import com.shengqi.xinnongren.work.ui.home.view.HomePage2View;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomePage2Fragment extends BaseMvpFragment<HomePage2View, HomePage2Presenter> implements HomePage2View, ChooseSubClassAdapter.OnItemClickListener, ViewPager.OnPageChangeListener {


    @BindView(R.id.rv_subclass)
    RecyclerView mRecyclerView;
    @BindView(R.id.ll_net_error)
    LinearLayout mLlError;

    GoodsListFragment mGoodsListFragment;

    @BindView(R.id.tl_class)
    TabLayout mTabLayout;
    List<String> titles = new ArrayList<>();
    List<String> subTitles = new ArrayList<>();

    List<ClassBean> mClassBeanList = new ArrayList<>();
    ChooseSubClassAdapter mChooseSubClassAdapter;

    List<Fragment> mFragmentList;
    private ClassBean mChoosedClassBean;

    public HomePage2Fragment() {
    }


    @Override
    protected int setLayoutResource() {
        return R.layout.fragment_home_page2;
    }


    @Override
    protected void initView() {
        mGoodsListFragment = (GoodsListFragment) getChildFragmentManager().findFragmentById(R.id.fragment_goods_list);
        mChooseSubClassAdapter = new ChooseSubClassAdapter(subTitles, getContext());
        mRecyclerView.setAdapter(mChooseSubClassAdapter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mChooseSubClassAdapter.setOnItemClickListener(this);
        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                setChildClass(tab.getPosition(), 0);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @Override
    protected void initData() {
        getPresenter().getClassList();
    }

    @Override
    protected void lazyLoadData() {
    }

    @Override
    protected HomePage2Presenter createPresenter() {
        return new HomePage2Presenter();
    }

    @Override
    public void showLoadingView() {

    }

    @Override
    public void hideLoadingView() {

    }

    @Override
    public void onNetworkError() {
        mLlError.setVisibility(View.VISIBLE);
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            Animator circularReveal = ViewAnimationUtils.createCircularReveal(mLlError, mLlError.getWidth() / 2, mLlError.getHeight() / 2, 0, mLlError.getHeight() / 1.5f);
            circularReveal.setDuration(600);
            circularReveal.setInterpolator(new DecelerateInterpolator());
            circularReveal.start();
        }
    }

    @Override
    public void onGetClassListSucceed(List<ClassBean> classBeanList) {
        mClassBeanList.clear();
        mClassBeanList.addAll(classBeanList);
        if (mClassBeanList.isEmpty())
            return;
//        mFragmentList = new ArrayList<>();
//        for (int i = 0; i < mClassBeanList.size(); i++) {
//            mFragmentList.add(GoodsListFragment.newInstance(mClassBeanList.get(i).getComId()));
//        }
//        mViewPager.setAdapter(new GeneralFragmentPagerAdapter(getChildFragmentManager(), mFragmentList));
//        mViewPager.setOffscreenPageLimit(mFragmentList.size());

        titles.clear();
        titles.addAll(ArrayUtil.listConvertToStringList(classBeanList, classBean -> classBean.getName()));
        mTabLayout.removeAllTabs();
        for (String title : titles) {
            mTabLayout.addTab(mTabLayout.newTab().setText(title));
        }
//        mChooseSubClassAdapter.notifyDataSetChanged();
//        mChooseSubClassAdapter.setCurChosePosition(0);
//        mViewPager.setOnPageChangeListener(this);
    }

    private void setChildClass(int position, int childPosition) {
        TabLayout.Tab tab = mTabLayout.getTabAt(position);
        if (tab == null) return;
        tab.select();
        mChoosedClassBean = mClassBeanList.get(position);
        subTitles.clear();
        for (ClassBean.SubClassBean subClassBean : mChoosedClassBean.getChildren()) {
            subTitles.add(subClassBean.getName());
        }
        mChooseSubClassAdapter.notifyDataSetChanged();
        mChooseSubClassAdapter.setCurChosePosition(childPosition);
    }

    @Override
    public void onItemClick(int position) {
//        Toast.makeText(getContext(), "" + position, Toast.LENGTH_SHORT).show();
//        mViewPager.setCurrentItem(position, true);
        ClassBean.SubClassBean subClassBean = mChoosedClassBean.getChildren().get(position);
        mGoodsListFragment.setClassInfo(String.format("%s,%s", mChoosedClassBean.getID(), subClassBean.getID()), subClassBean.getChildren());
    }


    @OnClick(R.id.tv_get_data)
    void tv_get_data() {
        mLlError.setVisibility(View.INVISIBLE);
        getPresenter().getClassList();
    }

    @OnClick(R.id.tv_search)
    void tv_search() {
        startActivity(new Intent(getContext(), SearchActivity.class));
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    }

    @Override
    public void onPageSelected(int position) {
        mChooseSubClassAdapter.setCurChosePosition(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    public void checkClassById(String classId) {
        if (mClassBeanList == null) return;
        for (int i = 0; i < mClassBeanList.size(); i++) {
            if (classId.equals(mClassBeanList.get(i).getID())) {
                setChildClass(i, 0);
                break;
            }
        }
    }

    public void checkSubClassById(String classId, String subClassId) {
        if (mClassBeanList == null) return;
        out:
        for (int i = 0; i < mClassBeanList.size(); i++) {
            ClassBean classBean = mClassBeanList.get(i);
            if (classId.equals(classBean.getID())) {
                List<ClassBean.SubClassBean> subClassBeanList = classBean.getChildren();
                for (int i1 = 0; i1 < subClassBeanList.size(); i1++) {
                    if (subClassId.equals(subClassBeanList.get(i1).getID())) {
                        setChildClass(i, i1);
                        break out;
                    }
                }
                break;
            }
        }
    }
}
